library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
dat <- read.csv('mobile-food-sf.csv', stringsAsFactors = FALSE)
day_freqs <- table(dat$DayOfWeekStr)
barplot(day_freqs, border = NA, las = 3)

plot_ly(x = names(day_freqs), 
        y = day_freqs,
        type = 'bar')
# day frequencies table
day_counts <- dat %>% 
  select(DayOfWeekStr) %>%
  group_by(DayOfWeekStr) %>%
  summarise(count = n()) %>%
  arrange(desc(count))

day_counts
## # A tibble: 7 x 2
##   DayOfWeekStr count
##   <chr>        <int>
## 1 Friday        1105
## 2 Wednesday     1095
## 3 Thursday      1090
## 4 Tuesday       1081
## 5 Monday        1080
## 6 Saturday       533
## 7 Sunday         263
plot_ly(day_counts, 
        x = ~DayOfWeekStr, 
        y = ~count,
        type = 'bar')
plot_ly(day_counts, 
        x = ~reorder(DayOfWeekStr, count), 
        y = ~count,
        type = 'bar')
library(stringr)
# toy string
time1 <- '10AM'
# hour
str_sub(time1, start = 1, end = 2)
## [1] "10"
# period
str_sub(time1, start = 3, end = 4)
## [1] "AM"
times <- c('12PM', '10AM', '9AM', '8AM', '2PM')

# subset time
str_sub(times, start = 1, end = -3)
## [1] "12" "10" "9"  "8"  "2"
# subset period
str_sub(times, start = -2, end = -1)
## [1] "PM" "AM" "AM" "AM" "PM"
# period
str_sub(times, start = -2)
## [1] "PM" "AM" "AM" "AM" "PM"
str_replace(times, pattern = 'AM|PM', replacement = '')
## [1] "12" "10" "9"  "8"  "2"
hours = str_sub(times, start = 1, end = -3)
periods = str_sub(times, start = -2, end = -1)

period_freqs <- table(periods)
plot_ly(x = names(period_freqs), 
        y = period_freqs,
        type = 'bar')
start24 = rep(0, length(dat$starttime))
for(i in 1:length(start24)) {
  hour = str_sub(dat$starttime[i], start = 1, end = -3)
  period = str_sub(dat$starttime[i], start = -2, end = -1)
  if(period == 'AM') {
    start24[i] = as.numeric(hour)
  } else if(period == 'PM' & hour == "12") {
    start24[i] = as.numeric(hour)
  } else if(period == 'PM') {
    start24[i] = as.numeric(hour) + 12
  }
}

end24 = rep(0, length(dat$starttime))
for(i in 1:length(end24)) {
  hour = str_sub(dat$endtime[i], start = 1, end = -3)
  period = str_sub(dat$endtime[i], start = -2, end = -1)
  if(period == 'AM') {
    end24[i] = as.numeric(hour)
  } else if(period == 'PM' & hour == "12") {
    end24[i] = as.numeric(hour)
  } else if(period == 'PM') {
    end24[i] = as.numeric(hour) + 12
  }
}

dat$start = paste(start24, "00", sep = ":")
dat$end = paste(end24, "00", sep = ":")
dat$duration = end24 - start24
for(i in 1: length(dat$duration)) {
  if(dat$duration[i] < 0) {
    dat$duration[i] = dat$duration[i]+12
  }
}
locs <- c(
  "(37.7651967350509,-122.416451692902)",
  "(37.7907890558203,-122.402273431333)",
  "(37.7111991003088,-122.394693339395)",
  "(37.7773000262759,-122.394812784799)",
  NA
)

lat_lon <- str_replace_all(locs, pattern = '\\(|\\)', replacement = '')
lat_lon = str_split(lat_lon, pattern = ',')


lat_lon <- str_replace_all(dat$Location, pattern = '\\(|\\)', replacement = '')
lat_lon = str_split(lat_lon, pattern = ',')

lat <- lapply(lat_lon, function(x) x[1])
lon <- lapply(lat_lon, function(x) x[2])
lat <- as.numeric(unlist(lat))
lon <- as.numeric(unlist(lon))
dat$lat = lat 
dat$lon = lon
library(RgoogleMaps)
# coordinates for center of the map
center <- c(mean(dat$lat, na.rm = TRUE), mean(dat$lon, na.rm = TRUE))

# zoom value
zoom <- min(MaxZoom(range(dat$lat, na.rm = TRUE), 
                    range(dat$lon, na.rm = TRUE)))

# san francisco map
map1 <- GetMap(center=center, zoom=zoom, destfile = "san-francisco.png")
PlotOnStaticMap(map1, dat$lat, dat$lon, col = "#ed4964", pch=20)

library(ggmap)
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
## 
##     wind
dat <- na.omit(dat)
# ggmap typically asks you for a zoom level, 
# but we can try using ggmap's make_bbox function:
sbbox <- make_bbox(lon = dat$lon, lat = dat$lat, f = .1)
sbbox
##       left     bottom      right        top 
## -122.48867   37.69985 -122.36281   37.81595
# get a 'terrain' map
sf_map <- get_map(location = sbbox, maptype = "terrain", source = "google")
## Warning: bounding box given to google - spatial extent only approximate.
## converting bounding box to center/zoom specification. (experimental)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.757897,-122.425744&zoom=13&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(sf_map) + 
  geom_point(data = dat, 
             mapping = aes(x = lon, y = lat), 
             color = "red", alpha = 0.2, size = 1)
## Warning: Removed 98 rows containing missing values (geom_point).

dat$optionaltext[1:3]
## [1] "Tacos, Burritos, Tortas, Quesadillas, Mexican Drinks, Aguas Frescas"   
## [2] "Cold Truck: sandwiches, drinks, snacks, candy, hot coffee"             
## [3] "Cold Truck: Pre-packaged Sandwiches, Various Beverages, Salads, Snacks"
foods <- dat$optionaltext[1:10]
grepl(foods, pattern = "burritos", ignore.case = TRUE)
##  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
grepl(foods, pattern = "tacos", ignore.case = TRUE)
##  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
grepl(foods, pattern = "quesadillas", ignore.case = TRUE)
##  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
foods <- dat$optionaltext
burritos = data.frame(
  burritos = grepl(foods, pattern = "burritos", ignore.case = TRUE)
)
burro_lat = dat$lat[burritos == TRUE]
burro_lon = dat$lon[burritos == TRUE]

# coordinates for center of the map
center <- c(mean(burro_lat, na.rm = TRUE), mean(burro_lon, na.rm = TRUE))
# zoom value
zoom <- min(MaxZoom(range(burro_lat, na.rm = TRUE), 
                    range(burro_lon, na.rm = TRUE)))
# burrito map
PlotOnStaticMap(map1, burro_lat, burro_lon, col = "#ed4964", pch=20)

burgers = data.frame(
  burgers = grepl(foods, pattern = "burger", ignore.case = TRUE)
)
burger_lat = dat$lat[burgers == TRUE]
burger_lon = dat$lon[burgers == TRUE]

# coordinates for center of the map
center <- c(mean(burger_lat, na.rm = TRUE), mean(burger_lon, na.rm = TRUE))
# zoom value
zoom <- min(MaxZoom(range(burger_lat, na.rm = TRUE), 
                    range(burger_lon, na.rm = TRUE)))
# burger map
PlotOnStaticMap(map1, burger_lat, burger_lon, col = "#ed4964", pch=20)

sandos = data.frame(
  sandos = grepl(foods, pattern = "sandwiches", ignore.case = TRUE)
)
sando_lat = dat$lat[sandos == TRUE]
sando_lon = dat$lon[sandos == TRUE]

# coordinates for center of the map
center <- c(mean(sando_lat, na.rm = TRUE), mean(sando_lon, na.rm = TRUE))
# zoom value
zoom <- min(MaxZoom(range(sando_lat, na.rm = TRUE), 
                    range(sando_lon, na.rm = TRUE)))
# sandwich map
PlotOnStaticMap(map1, sando_lat, sando_lon, col = "#ed4964", pch=20)

pizza = data.frame(
  pizza = grepl(foods, pattern = "pizza", ignore.case = TRUE)
)
pizza_lat = dat$lat[pizza == TRUE]
pizza_lon = dat$lon[pizza == TRUE]

# coordinates for center of the map
center <- c(mean(pizza_lat, na.rm = TRUE), mean(pizza_lon, na.rm = TRUE))
# zoom value
zoom <- min(MaxZoom(range(pizza_lat, na.rm = TRUE), 
                    range(pizza_lon, na.rm = TRUE)))
# pizza map
PlotOnStaticMap(map1, pizza_lat, pizza_lon, col = "#ed4964", pch=20)